<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery-3.7.1.min.js"></script>
</head>
<body>
<div class="container-fluid">
    这就是最基本的页面

    <table class="table table-bordered">
        <thead>
        <tr>
            <th>#</th>
            <th>主键</th>
            <th>姓名</th>
            <th>学号</th>
            <th style="width: 100px;">操作</th>
        </tr>
        </thead>
        <tbody id="tb_student">

        </tbody>
    </table>

    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        新增学生
    </button>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">
                <form id="formTeacher">

                    <input hidden="hidden" id="id" />
                    <div class="form-group">
                        <label for="id">主键</label>
                        <input type="text" class="form-control" id="id" name="id" placeholder="主键">
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="number">学号</label>
                        <input type="text" class="form-control" id="number" name="number" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option>男</option>
                            <option>女</option>
                        </select>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="addStudent()">保存</button>
            </div>
        </div>
    </div>
</div>

<script src="/js/bootstrap.min.js"></script>

<script type="text/javascript">
    function addStudent() {
        var data = $("#tb_student").serialize();

        $.ajax({
            url: "/webapi/student/insert",
            method: "post",
            data: data
        }).done(function () {
            loadTable();
            $('#myModal').modal('hide');
        });
    }

    function editStudent(id) {
        $('#myModal').modal('show');

        $.ajax({
            url: '/webapi/student/get/' + id
        }).done(function (rs) {
            $("#id").val(rs.id);
            $("#number").val(rs.number);
            $("#name").val(rs.name);
            $("#sex").val(rs.sex);
        });
    }

    function loadTable() {
        $.ajax({
            url: '/webapi/student/list'
        }).done(function (rs) {
            var len = rs.length;
            var html = "";
            for (var i = 0; i < len; i++) {
                var item = rs[i];
                html += "<tr>"
                    + "<td>" + item.id + "</td>"
                    + "<td>" + item.name + "</td>"
                    + "<td>" + item.number + "</td>"
                    + "<td> <a href='#' onclick='editStudent(" + item.id + ");'>编辑</a></td>"
                    + "</tr>";
            }
            $('#tb_student').html(html);
        });
    }

    $(function () {
        loadTable();
    });
</script>
</body>
</html>
